﻿using Equipment_manage.Models;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;

namespace Equipment_manage.DAL
{
    public class UsersServices
    {
        private static Equipment DB = new Equipment();
        private static DbSet<Users> table = DB.Users;
        public static bool Add(Users user)
        {
            try {
                table.Add(user);
               var o= DB.SaveChanges();
                return true; }
            catch { return false; }
        }
        public static bool Delet(int e_ID)
        {
            try
            {
                var  record=table.FirstOrDefault(p => p.e_ID == e_ID);
                table.Remove(record);
                DB.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static bool IsHead(int e_id)
        {
           var rd= table.FirstOrDefault(p => p.e_ID == e_id);
           if (rd.u_Type != "downer" && rd.u_Type != "wowner")
               return false;
           else
               return true;
        }
        public static bool AlertPower(int id, string power)
        {

            table.FirstOrDefault(p => p.e_ID == id).u_Type = power;
            return DB.SaveChanges() == 1;
        }
        public static bool ALertpwd(int e_id, string pwd)
        {
            var user = table.FirstOrDefault(p => p.e_ID == e_id);
            if (user.u_Password != pwd)
            {
                user.u_Password = pwd;
                DB.SaveChanges();
                return true;
            }
            else
                return false;
        }
        public static List<Users> GetServiceman()
        {
            var all = table.Where(p => p.u_Type == "serviceman");
            return table.Where(p => p.u_Type == "serviceman").ToList();
        }
    }
}